package com.zjcloud.jwgl.domain;

import com.zjcloud.common.annotation.Excel;
import com.zjcloud.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 约束参数对象
 * 支持约束条件的动态参数配置
 *
 * @author zjcloud
 * @since 2024-01-01
 */
public class AcConstraintParameter extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /** 主键ID */
    private Long id;

    /** 约束条件ID */
    @Excel(name = "约束条件ID")
    @NotNull(message = "约束条件ID不能为空")
    private Long constraintId;

    /** 参数键 */
    @Excel(name = "参数键")
    @NotBlank(message = "参数键不能为空")
    private String paramKey;

    /** 参数值 */
    @Excel(name = "参数值")
    private String paramValue;

    /** 参数类型：STRING-字符串，INTEGER-整数，DECIMAL-小数，BOOLEAN-布尔值，JSON-JSON对象 */
    @Excel(name = "参数类型")
    @NotBlank(message = "参数类型不能为空")
    private String paramType;

    /** 参数描述 */
    @Excel(name = "参数描述")
    private String description;

    /** 是否必填：1-是，0-否 */
    @Excel(name = "是否必填")
    private String isRequired;

    /** 默认值 */
    @Excel(name = "默认值")
    private String defaultValue;

    /** 验证规则（正则表达式或JSON格式） */
    private String validationRule;

    /** 排序号 */
    @Excel(name = "排序号")
    private Integer sortOrder;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setConstraintId(Long constraintId) {
        this.constraintId = constraintId;
    }

    public Long getConstraintId() {
        return constraintId;
    }

    public void setParamKey(String paramKey) {
        this.paramKey = paramKey;
    }

    public String getParamKey() {
        return paramKey;
    }

    public void setParamValue(String paramValue) {
        this.paramValue = paramValue;
    }

    public String getParamValue() {
        return paramValue;
    }

    public void setParamType(String paramType) {
        this.paramType = paramType;
    }

    public String getParamType() {
        return paramType;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setIsRequired(String isRequired) {
        this.isRequired = isRequired;
    }

    public String getIsRequired() {
        return isRequired;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setValidationRule(String validationRule) {
        this.validationRule = validationRule;
    }

    public String getValidationRule() {
        return validationRule;
    }

    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }

    public Integer getSortOrder() {
        return sortOrder;
    }

    /**
     * 判断是否必填
     */
    public boolean isRequired() {
        return "1".equals(this.isRequired);
    }

    /**
     * 获取类型化的参数值
     */
    public Object getTypedValue() {
        if (paramValue == null) {
            return defaultValue;
        }
        
        switch (paramType.toUpperCase()) {
            case "INTEGER":
                try {
                    return Integer.valueOf(paramValue);
                } catch (NumberFormatException e) {
                    return defaultValue != null ? Integer.valueOf(defaultValue) : 0;
                }
            case "DECIMAL":
                try {
                    return Double.valueOf(paramValue);
                } catch (NumberFormatException e) {
                    return defaultValue != null ? Double.valueOf(defaultValue) : 0.0;
                }
            case "BOOLEAN":
                return Boolean.valueOf(paramValue);
            case "JSON":
                return paramValue; // 返回JSON字符串，由调用方解析
            case "STRING":
            default:
                return paramValue;
        }
    }

    /**
     * 设置类型化的参数值
     */
    public void setTypedValue(Object value) {
        if (value == null) {
            this.paramValue = null;
            return;
        }
        this.paramValue = value.toString();
    }

    /**
     * 获取参数类型描述
     */
    public String getParamTypeDesc() {
        switch (paramType) {
            case "STRING":
                return "字符串";
            case "INTEGER":
                return "整数";
            case "DECIMAL":
                return "小数";
            case "BOOLEAN":
                return "布尔值";
            case "JSON":
                return "JSON对象";
            default:
                return "未知类型";
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("constraintId", getConstraintId())
                .append("paramKey", getParamKey())
                .append("paramValue", getParamValue())
                .append("paramType", getParamType())
                .append("description", getDescription())
                .append("isRequired", getIsRequired())
                .append("defaultValue", getDefaultValue())
                .append("validationRule", getValidationRule())
                .append("sortOrder", getSortOrder())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("remark", getRemark())
                .toString();
    }
}